/**
 * Class: OpenLayers.WMSQueryHandler
 * WMS图层查询处理对象,空间数据查询由wfs实现
 *  
 * 继承自:
 *  - <OpenLayers.Class> 
 */
OpenLayers.ArcRestWMSQueryHandler=OpenLayers.Class({
	querytype: "spetial",
	
	layerstitles: null,
	
	proxy: null,
	
    initialize: function(qos,queryendMethod,options) {
		this.queryObjectList=qos;
		this.queryend=queryendMethod;
		OpenLayers.Util.extend(this,options);
    },
    //根据组织的查询xml提交至服务器查询
    requestData:function(evt){
    	var wmshander=this;
    	var queryURLs = this.createQueryUrls();
    	this.qevent = evt;
    	var results = [];
    	var index = 0;
    	for(var i in queryURLs){
    		var url = queryURLs[i];
    		var params = null;
    		
    		if(this.proxy){
    			url = this.proxy+"?url="+escape(url);
    		}
    		
    		OpenLayers.Request.issue({
		        url: url, params: params, async: true, method: "POST", data: null, 
		        success: function(req){
		        	index++;
    				var result = null;
    				if(req.responseText != ""){
    					result = eval("("+req.responseText+")");
    				}
    				results.push(result);
		  	    	if(index == queryURLs.length){
		  	    		wmshander.outputdata(results);
		  	    	}
		        }
		    });
    	}
    },
    //查询返回的数据处理
    outputdata:function(results){
    	if(this.queryend!=null){
    		//设置了二次开发查询后回调
    		this.queryend(results, this.qevent);
    	}else{
    		//默认的查询后数据展示
    		//alert(resultxml);
    	}
    	//数据加载完成后移除滚动条
    },
    
	//构造查询的xml文件
    createQueryUrls:function(){
    	var queryurls = [];
    	this.layerstitles = [];
    	var wmsqueryObjects = this.queryObjectList;
    	if(wmsqueryObjects!=null&&wmsqueryObjects.length>0){
    		if(this.querytype == "spetial"){
			    for(var i=0; i< wmsqueryObjects.length; i++){
			    	var subLayers = wmsqueryObjects[i].subLayers.split(",");
			    	var url = wmsqueryObjects[i].url;
			    	var coords = wmsqueryObjects[i].coords;
			    	var baseurl = url.replace("/arcgis/","/arcgis/rest/");
			    	baseurl = baseurl.replace("/WMSServer","");
			        for(var j in subLayers){
			        	var layername = subLayers[j];
			        	var queryurl = baseurl +'/'+layername+'/query?';
			        	queryurl += 'text=&geometry='+coords+'&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&returnIdsOnly=false&returnGeometry=true&outFields=*&f=json';
			        	queryurl = queryurl.replace(/\,/g,"%2C").replace(/\s/g,"+");
			        	queryurls.push(queryurl);
			        }
		        }
	        }else{
	        	for(var i=0;i<wmsqueryObjects.length;i++){
	        		if(wmsqueryObjects[i].where && typeof(wmsqueryObjects[i].where) == "object"){
	        			var where = wmsqueryObjects[i].where;
		        		var subLayers = wmsqueryObjects[i].subLayers;
		        		var url = wmsqueryObjects[i].wmslayerURL;
		        		var baseurl = url.replace("/arcgis/","/arcgis/rest/");
				    	baseurl = baseurl.replace("/WMSServer","");
				    	
				    	var queryurl = baseurl+ "/find?searchText=" + where.searchtext+"&contains=true&searchFields="+where.fields+"&layers="+subLayers+"&f=json";
				    	queryurl = queryurl.replace(/\,/g,"%2C").replace(/\s/g,"+");
				    	queryurls.push(queryurl);
	        		}
	        	}
	    	}
    	}
        return queryurls;
    },
    
    CLASS_NAME: "OpenLayers.ArcRestWMSQueryHandler"
});